package cn.hd.controller;

import cn.hd.entity.Product;
import cn.hd.entity.Result;
import cn.hd.entity.User;
import cn.hd.service.ProductService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class ProductController {
    @Autowired
    private ProductService productService;

//    @RequestMapping("/addProduct")
//    public Object addProduct(Product product,String dd){
////        product.setName("admin");
//        if (dd.contains(",")){
//            String s = dd.substring(dd.lastIndexOf(",")+1); //二级分类
//            int pow2 = Integer.parseInt(s);
//            product.setSecondclass(pow2);
//            String s1=dd.substring(0,1);
//            int pow1=Integer.parseInt(s1);
//            product.setFirstclass(pow1);
//        }else{
//            String power1=dd.substring(0,1);
//            int power=Integer.parseInt(power1);
//            product.setFirstclass(power);
//            product.setSecondclass(0);
//        }
//
//        int i = productService.addProduct(product);
//        if(i!=0){
//            return new Result("200","添加成功",null);
//        }else{
//            return new Result("400","添加失败",null);
//        }
//    }

    @RequestMapping("/delProduct")
    public Object delProduct(long id){
        int i = productService.delProduct(id);
        if(i!=0){
            return new Result("200","删除成功",null);
        }
        return new Result("400","删除失败",null);
    }

    @RequestMapping("/selectByNameProduct")
    public Object selectByNameProduct(String name){
        Map map=new HashMap();
        map.put("name",name);

        List<Product> list = productService.selectByNameProduct(map);
        if (list.size()==0){
            return new Result("400","查无此数据",null);
        }
        return new Result("200","查询成功",list);
    }
}
